/*
 * ImageConverter.java
 *
 * $Id: ImageConverter.java,v 1.1 2008-03-18 12:58:46 mario Exp $
 */
package org.ceteca.explica.core.util;

import java.awt.image.BufferedImage;
import java.awt.image.DirectColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;

import javax.swing.ImageIcon;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * Converts an AWT/Swing image to its correspondent image object in SWT.
 * <br/> 
 * Responsibilities:
 * <ul>
 * <li>Convert images into SWT compliant image objects. </li>
 * </ul>
 * <br/>
 * 
 * @author Mario García García <mario@imagos.es> Copyright (c) 2007 Fundación
 *         Centro Tecnolóxico da Carne This program is free software: you can
 *         redistribute it and/or modify it under the terms of the GNU General
 *         Public License as published by the Free Software Foundation, either
 *         version 3 of the License, or (at your option) any later version.
 *         <br/> This program is distributed in the hope that it will be useful,
 *         but WITHOUT ANY WARRANTY; without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/> You should have
 *         received a copy of the GNU General Public License along with this
 *         program. If not, see <http://www.gnu.org/licenses/>.
 */
public class ImageConverter {
	/**
	 * Converts an AWT BufferedImage to SWT format. The library Barbecue, used
	 * for the barcode generation, only generates images in AWT format. So
	 * a mechanism for converting them into the SWT (GUI library used
	 * in the application) is needed.
	 * @param bufferedImage BufferedImage, AWT BufferedImage to convert.
	 * @return ImageData, SWT formatted image.
	 * <br/>
	 * Copyright (c) 2000, 2004 IBM Corporation and others.
	 * All rights reserved. This program and the accompanying materials
	 * are made available under the terms of the Eclipse Public License v1.0
	 * which accompanies this distribution, and is available at
	 * http://www.eclipse.org/legal/epl-v10.html
	 *
	 * Contributors:
	 *     IBM Corporation - initial API and implementation
	 */
	public static ImageData convertToSWT(BufferedImage bufferedImage) {
		if (bufferedImage.getColorModel() instanceof DirectColorModel) {
			DirectColorModel colorModel = (DirectColorModel) bufferedImage
					.getColorModel();
			PaletteData palette = new PaletteData(colorModel.getRedMask(),
					colorModel.getGreenMask(), colorModel.getBlueMask());
			ImageData data = new ImageData(bufferedImage.getWidth(),
					bufferedImage.getHeight(), colorModel.getPixelSize(),
					palette);
			WritableRaster raster = bufferedImage.getRaster();
			int[] pixelArray = new int[3];
			for (int y = 0; y < data.height; y++) {
				for (int x = 0; x < data.width; x++) {
					raster.getPixel(x, y, pixelArray);
					int pixel = palette.getPixel(new RGB(pixelArray[0],
							pixelArray[1], pixelArray[2]));
					data.setPixel(x, y, pixel);
				}
			}
			return data;
		} 
		else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
			IndexColorModel colorModel = (IndexColorModel) bufferedImage
					.getColorModel();
			int size = colorModel.getMapSize();
			byte[] reds = new byte[size];
			byte[] greens = new byte[size];
			byte[] blues = new byte[size];
			colorModel.getReds(reds);
			colorModel.getGreens(greens);
			colorModel.getBlues(blues);
			RGB[] rgbs = new RGB[size];
			for (int i = 0; i < rgbs.length; i++) {
				rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
						blues[i] & 0xFF);
			}
			PaletteData palette = new PaletteData(rgbs);
			ImageData data = new ImageData(bufferedImage.getWidth(),
					bufferedImage.getHeight(), colorModel.getPixelSize(),
					palette);
			data.transparentPixel = colorModel.getTransparentPixel();
			WritableRaster raster = bufferedImage.getRaster();
			int[] pixelArray = new int[1];
			for (int y = 0; y < data.height; y++) {
				for (int x = 0; x < data.width; x++) {
					raster.getPixel(x, y, pixelArray);
					data.setPixel(x, y, pixelArray[0]);
				}
			}
			return data;
		}
		return null;
	}
	
	public static ImageData convertToSWT(Display display, byte[] imgData) {
	    Color white = display.getSystemColor(SWT.COLOR_WHITE);
	    Color black = display.getSystemColor(SWT.COLOR_BLACK);
	    Color yellow = display.getSystemColor(SWT.COLOR_YELLOW);
	    Color red = display.getSystemColor(SWT.COLOR_RED);
	    Color green = display.getSystemColor(SWT.COLOR_GREEN);
	    Color blue = display.getSystemColor(SWT.COLOR_BLUE);

	    // Create a source ImageData of depth 4
	    PaletteData palette = new PaletteData(new RGB[] { black.getRGB(), white.getRGB(),
	        yellow.getRGB(), red.getRGB(), blue.getRGB(), green.getRGB() });
	    ImageData data = new ImageData(16, 16, 4, palette, 1, imgData);
	    
	    return data;
	}
	
	public static ImageData convertToSWT(byte[] imgData) {
		return convertToSWT((BufferedImage)new ImageIcon(imgData).getImage());
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ImageConverter.java,v 1.1 2008-03-18 12:58:46 mario Exp $";
	}
}
